package com.example.chinalin.viewpagerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.example.chinalin.viewpagerdemo.viewpager.MyJazzyViewPager;
import com.nineoldandroids.view.ViewHelper;

public class DemoActivity extends Activity {

    protected static final String TAG = "MainActivity";
    private int[] mImgIds;
    private MyJazzyViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImgIds = new int[] { R.drawable.image_1, R.drawable.image_2, R.drawable.image_3,
                R.drawable.image_4 };
        mViewPager = (MyJazzyViewPager) findViewById(R.id.id_viewPager);

        mViewPager.setAdapter(new PagerAdapter()
        {

            @Override
            public boolean isViewFromObject(View arg0, Object arg1)
            {
                return arg0 == arg1;
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object)
            {
                container.removeView((View) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position)
            {
                ScrollView scrollView = new ScrollView(DemoActivity.this);
                setupScrollView(scrollView);
                container.addView(scrollView, ViewGroup.MarginLayoutParams.MATCH_PARENT, ViewGroup.MarginLayoutParams.MATCH_PARENT);
                mViewPager.setObjectForPosition(scrollView, position);
                return scrollView;
            }

            @Override
            public int getCount()
            {
                return 10;
            }
        });

    }
    private void setupRelayout(RelativeLayout relativeLayout, int position){
        addImageView(relativeLayout,position);
        addButton(relativeLayout,position);
    }
    private  void addButton(RelativeLayout relativeLayout,int position){
                Button button = new Button(this) ;

//                RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(300,300);
//                lp.setMargins(i*300,j*300, 0, 0);
//                button.setLayoutParams(lp);
//                button.requestLayout();
                button.setText("button " + position);
                relativeLayout.addView(button);
    }

    private  void addImageView(RelativeLayout relativeLayout, int position){
        ImageView imageView = new ImageView(DemoActivity.this);
        imageView.setImageResource(mImgIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        relativeLayout.addView(imageView);
    }



    private void setupScrollView(ScrollView scrollView){
        //        scrollView.setScrollBarStyle(Scrollview.none);
        HorizontalScrollView horizontalScrollView = new HorizontalScrollView(DemoActivity.this);
        RelativeLayout relativeLayout = new RelativeLayout(DemoActivity.this);
        setupRelayout(relativeLayout);
        horizontalScrollView.addView(relativeLayout);
        scrollView.addView(horizontalScrollView);
    }
    //    private void setupRelayout(){
    //        setupRelayout(ViewPagerScrollActivity.this.relativeLayout) ;
    //    }
    private void setupRelayout(RelativeLayout relativeLayout){
        addButton(relativeLayout);
    }
    private  void addButton(RelativeLayout relativeLayout){
        for (int i=0; i<4 ; i++){
            for (int j=0;j<2 ;j++){
                Button button = new Button(this) ;

                RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(300,300);
                lp.setMargins(i*300,j*300, 0, 0);
                button.setLayoutParams(lp);
                //            imageView.setLayoutParams(layoutParams);
                button.requestLayout();
                button.setText(i*10+j + "");
                relativeLayout.addView(button);
            }
        }
    }

    private  void addImageView(RelativeLayout relativeLayout){
        for (int i=0; i<10 ; i++){
            for (int j=0;j<10 ;j++){
                ImageView imageView = new ImageView(this) ;
                imageView.setFocusable(true);
                //                Glide
                //                        .with(ViewPagerScrollActivity.this)
                //                        .load(constant.eatFoodyImages[(i+j)%14])
                //                        .into(imageView);
                imageView.setImageResource(R.drawable.image_1);
                RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(300,300);
                lp.setMargins(i*300,j*300, 0, 0);
                imageView.setLayoutParams(lp);
                //            imageView.setLayoutParams(layoutParams);
                imageView.requestLayout();
                imageView.setId(i*10+j);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(DemoActivity.this,"id:"+v.getId(),Toast.LENGTH_SHORT).show();
                    }
                });
                imageView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus){
                            ImageView iv = (ImageView)v;
                            iv.setScaleType(ImageView.ScaleType.FIT_XY);
                            ViewHelper.setScaleX(v, 1.1f);
                            ViewHelper.setScaleY(v, 1.1f);
                        }else{
                            ImageView iv = (ImageView)v;
                            iv.setScaleType(ImageView.ScaleType.CENTER);
                            //                            ViewHelper.setScaleX(v, 1.0f);
                            //                            ViewHelper.setScaleY(v, 1.0f);
                        }
                    }
                });
                relativeLayout.addView(imageView);
            }
        }
    }
}
